<template>
  <div class="container">
    <div class="login-wrapper">
      <div class="header">欢迎登录</div>
      <div class="form-wrapper">
        <input
          type="text"
          v-model="phone"
          name="userphone"
          placeholder="请输入手机号"
          class="input-item"
        />
        <input
          type="password"
          v-model="pass"
          name="userpass"
          placeholder="请输入密码"
          class="input-item"
        />
        <router-link to="/" class="toadmin">返回首页</router-link>
        <button class="btn" @click="loginEvent">登录</button>
        <button class="btn" @click="registerEvent">注册</button>
      </div>
    </div>
    <el-dialog custom-class="addDialog" title="注册" :visible.sync="showAddBox">
      <el-form :model="form">
        <el-form-item label="手机号" labelWidth="50">
          <el-input v-model="phone" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="密码   " labelWidth="50">
          <el-input v-model="pass" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="用户名" labelWidth="50">
          <el-input v-model="name" autocomplete="off"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="showAddBox = false">取 消</el-button>
        <el-button type="primary" @click="sendAdd">确认新增</el-button>
      </div>
    </el-dialog>
  </div>
</template>
 
<script>
import $http from "../http/index.js";
export default {
  data() {
    return {
      phone: "",
      pass: "",
      name:'',
      input: "",
      showAddBox: false,
    
    };
  },

  methods: {
    async loginEvent() {
      let res = await $http.userHttp.login({
        userphone: this.phone,
        userpass: this.pass,
      });
      console.log(res);
      if (res.data.message == "登录成功") {
        localStorage.setItem("LoginUser", this.phone); //保存登录用户手机号
        this.$router.push({
          path: "/admin/blog",
        });
      } else {
        alert("登录失败");
      }
    },

    registerEvent() {
      this.showAddBox = true;
    },
    async sendAdd() {
      //确认新增
      let obj = {
        username:this.name,
        userpass: this.pass,
        userphone: this.phone,
        
      }
      let res = await this.$http.userHttp.addUser(obj);
      console.log(res)
      if (res.data == "注册成功") {
        this.$message({
          message: res.data,
          type: "success",
        });
        this.showAddBox = false;
        
      } else {
        this.$message({
          message: res.data,
          type: "error",
        });
      }
    },
  },
};
</script>

<style scoped lang="scss" scoped>
html {
  height: 100%;
}
body {
  height: 100%;
}
.container {
  /* margin-top: 5%; */
  height: 980px;
  width: 100%;
  background-image: linear-gradient(to right, #8b88f2, #1261e8);
}
.login-wrapper {
  background-color: #ffffff;
  width: 358px;
  height: 588px;
  border-radius: 15px;
  padding: 0 50px;
  position: absolute;
  left: 50%;
  top: 50%;
  transform: translate(-50%, -50%);
}
.header {
  font-size: 38px;
  font-weight: bold;
  text-align: center;
  line-height: 200px;
}
.input-item {
  display: block;
  width: 100%;
  margin-bottom: 20px;
  border: 0;
  padding: 10px;
  border-bottom: 1px solid rgb(128, 125, 125);
  font-size: 15px;
  outline: none;
}
.input-item:placeholder {
  text-transform: uppercase;
}
.btn {
  text-align: center;
  padding: 15px;
  width: 90%;
  margin-top: 40px;
  margin-left: 20px;
  background-image: linear-gradient(to right, #8b88f2, #1261e8);
  color: #ffffff;
  
}
.toadmin {
  text-align: left;
  margin-left: 0px;
  margin-top: 30px;
}

.msg {
  text-align: center;
  line-height: 88px;
}
a {
  text-decoration-line: none;
  color: #abc1ee;
}
::v-deep .addDialog{
    .el-input{
        width: 400px;
    }
    .el-input__inner{
        width: 400px;
    }
    .el-form-item__content{
        width: 460px;
    }
    .el-form-item{
        margin-left: 200px;
    }
}
</style>